# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： template_process
Description :
Author : 'li'
date： 2020/11/5
-------------------------------------------------
Change Activity:
2020/11/5:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.common_template import \
    CommonTemplate
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.four_template import \
    FourRowTemplate
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.one_row_template import \
    NumInOneRowTemplate
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.three_rows_template import \
    ThreeRowTemplate
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.two_rows_template import \
    TwoRowTemplate


class TemplateProcessor:
    def __init__(self, format_result_list):
        self.format_result_list = format_result_list
        self.template_type_templates = self._get_template_type()

    def _get_template_type(self):
        """
        :return:
        """
        templates = []
        template = NumInOneRowTemplate(self.format_result_list)
        if template.is_this_template():
            templates.append(template)
        template = TwoRowTemplate(self.format_result_list)
        if template.is_this_template():
            templates.append(template)
        template = ThreeRowTemplate(self.format_result_list)
        if template.is_this_template():
            templates.append(template)
        template = FourRowTemplate(self.format_result_list)
        if template.is_this_template():
            templates.append(template)
        if len(templates) > 0:
            return templates
        template = CommonTemplate(self.format_result_list)
        if len(templates) == 0:
            return [template]

    def get_result(self):
        if len(self.template_type_templates) == 0:
            return
        current_template = None
        for template_type_template in self.template_type_templates:
            template_type_template.get_result()
            if template_type_template.is_valid_container_number:
                return template_type_template
            current_template = template_type_template
        return current_template
